import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/views/index'
import Login from '@/views/login'
// import store from '@/store'
// import {isURL} from '@/utils/val
import Cookies from 'js-cookie'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: '/',
      component: Index
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {showBottomTabBar: true}
    },
    {
      path: '/index',
      name: 'index',
      component: Index
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.name === 'List') {
    next()
  } else {
    let token = Cookies.get('token')
    // let userName = sessionStorage.getItem('user')
    if (to.path === '/login') {
      // 如果是访问登录界面，如果用户会话信息存在，代表已登录过，跳转到主页
      if (token) {
        next({ path: '/index' })
      } else {
        next()
      }
    } else {
      if (!token) {
        // 如果访问非登录界面，且户会话信息不存在，代表未登录，则跳转到登录界面
        next({ path: '/login' })
      } else {
        // 加载动态菜单和路由
        next()
      }
    }
  }
})

export default router
